package pers.qianyu.month_202102.date_20210213;

import org.junit.*;

/**
 * https://leetcode-cn.com/problems/string-to-url-lcci/
 * 面试题 01.03. URL化
 *
 * @author mizzle rain
 * @date 2021-02-13 20:38
 */
public class ReplaceSpaces {
    public String replaceSpaces(String S, int length) {
        char[] arr = S.toCharArray();
        int cnt = 0;
        for (int i = 0; i < length; i++) if (arr[i] == ' ') cnt++;
        for (int j = length - 1, i = length + 2 * cnt - 1; i >= 0; i--, j--) {
            if (arr[j] != ' ') {
                arr[i] = arr[j];
            } else {
                arr[i--] = '0';
                arr[i--] = '2';
                arr[i] = '%';
            }
        }
        return new String(arr, 0, length + 2 * cnt);
    }

    @Test
    public void test1() {
        String result = new ReplaceSpaces()
                .replaceSpaces("ds sdfs afs sdfa dfssf asdf             ", 27);
        System.out.println("result = " + result);
    }
}
